package com.shop.shopserver.controller.user;


import com.shop.shopcommon.constant.SubmitOrderMode;
import com.shop.shopcommon.enumeration.PayModeType;
import com.shop.shopcommon.result.Result;
import com.shop.shoppojo.dto.OrderDTO;
import com.shop.shoppojo.dto.PayDTO;
import com.shop.shoppojo.dto.SubmitOrderDTO;
import com.shop.shoppojo.dto.UpdateOrderDTO;
import com.shop.shoppojo.vo.OrderVO;
import com.shop.shoppojo.vo.SubmitOrderVO;
import com.shop.shoppojo.vo.UnSubmittedOrdersDataVO;
import com.shop.shoppojo.vo.UpdateOrderVO;
import com.shop.shopserver.service.impl.OrderServiceImpl;
import com.shop.shopserver.service.impl.PayServiceImpl;
import com.shop.shopserver.service.impl.SubmitOrderServiceImpl;
import com.shop.shopserver.service.impl.UpdateOrderStatusServiceImpl;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("checkout")
@Slf4j
public class TradeController {
    @Autowired
    private PayServiceImpl payServiceImpl;

    @Autowired
    private OrderServiceImpl orderServiceImpl;

    @Autowired
    private SubmitOrderServiceImpl submitOrderServiceImpl;

    @Autowired
    private UpdateOrderStatusServiceImpl updateOrderStatusServiceImpl;

    /**
     * 订单结算
     *
     * @param payDTO
     * @return
     * @throws IOException
     */

    @GetMapping("goPay")
    @ApiOperation("订单结算")
    public Result<UnSubmittedOrdersDataVO> goPayCart(PayDTO payDTO) throws IOException {
        // 购物车跳转支付 带cartIds字符串
        if (payDTO.getMode().equals(SubmitOrderMode.CART)) {
             return payServiceImpl.goPayCart(payDTO);
        }
        // 直接购买跳转支付 带goodsId
        return payServiceImpl.goPayProDetail(payDTO);
    }

    /**
     * 获取我的订单列表
     *
     * @param orderDTO
     * @return
     * @throws IOException
     */

    @GetMapping("getMyOrderList")
    @ApiOperation("获取我的订单列表")
    public Result<OrderVO> getMyOrderList(OrderDTO orderDTO) throws IOException {
        return orderServiceImpl.getMyOrderList(orderDTO);
    }


    /**
     * 提交订单
     *
     * @param submitOrderDTO
     * @return
     * @throws IOException
     */
    @PostMapping("submitOrder")
    @ApiOperation("提交订单")
    public Result<List<SubmitOrderVO>> submitOrder(@RequestBody SubmitOrderDTO submitOrderDTO) throws Exception {
        System.out.println(submitOrderDTO);
        if (Objects.equals(submitOrderDTO.getMode(), SubmitOrderMode.BUY_NOW)) {
            System.out.println("buyNow");
            return submitOrderServiceImpl.submitOrderGoodsId(submitOrderDTO);
        } else {
            System.out.println("cart");
            return submitOrderServiceImpl.submitOrderCart(submitOrderDTO);
        }
    }

    /**
     * 支付订单
     * @param updateOrderDTO
     * @return
     * @throws IOException
     */

    //立刻付款 待支付->待发货
    @PostMapping("order/payOrder")
//    @RequestLock(prefix = "payOrder_post_")
    public Result<UpdateOrderVO> payOrder(@RequestBody UpdateOrderDTO updateOrderDTO) throws IOException {
        return updateOrderStatusServiceImpl.payOrder(updateOrderDTO);
    }


    /**
     * 取消订单
     * @param updateOrderDTO
     * @return
     */
    //申请取消订单
    @PostMapping("/order/applyCancelOrder")
//    @RequestLock(prefix = "applyCancelOrder_post_")
    public Result<UpdateOrderVO> applyCancelOrder(@RequestBody UpdateOrderDTO updateOrderDTO){
        System.out.println(updateOrderDTO);
        return updateOrderStatusServiceImpl.applyCancelOrder(updateOrderDTO);
        //有订单 删除

    }


    /**
     * 确认收货
     * @param updateOrderDTO
     * @return
     */
    //确认收货 待收货->已收货(待评价)
    @PostMapping("/order/confirmReceipt")
//    @RequestLock(prefix = "confirmReceipt_post_")
    public Result<UpdateOrderVO> confirmReceipt(@RequestBody UpdateOrderDTO updateOrderDTO){
        return updateOrderStatusServiceImpl.confirmReceipt(updateOrderDTO);
        //有订单 order_status=3->4
    }


    /**
     * 发货
     * @param updateOrderDTO
     * @return
     */
    //发货 代发货->待收货
    @PostMapping("/order/deliverOrder")
//    @RequestLock(prefix = "deliverOrder_post_")
    public Result<UpdateOrderVO> deliverOrder(@RequestBody UpdateOrderDTO updateOrderDTO){
        return updateOrderStatusServiceImpl.deliverOrder(updateOrderDTO);
        //有订单 delivery_status=2->3
    }


    /**
     * 删除订单
     * @param updateOrderDTO
     * @return
     */
    // 删除订单（已取消->删除）
    @PostMapping("/order/deleteOrder")
    public Result<UpdateOrderVO> deleteOrder(@RequestBody UpdateOrderDTO updateOrderDTO){
        return updateOrderStatusServiceImpl.deleteOrder(updateOrderDTO);
    }
}
